﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;
using System.Data;

namespace Iacym.Repository
{
    public class CargoRepository:MasterRepository,ICargoRepository
    {
        public List<CARGO> GetAllFromCargo()
        {
            var query = from c in context.CARGOes
                        orderby c.Descripcion ascending 
                        select c;

            return query.ToList();
        }

        public List<CARGO> GetFromCargoByDescripcion(string descripcion)
        {
            if (descripcion != "")
            {
                var query = from c in context.CARGOes
                            where c.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in context.CARGOes
                            select c;
                return query1.ToList();
            }
        }

        public bool ExistDuplicidadCargo(int id, string descripcion)
        {
            var query = from c in context.CARGOes
                        where c.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && c.Id != id
                        select c;

            return query.Any();
        }


        public void AddCargo(CARGO cargo)
        {
            context.CARGOes.Add(cargo);
            context.SaveChanges();
        }

        public void UpdateCargo(CARGO cargo)
        {
            context.CARGOes.Attach(cargo);
            context.Entry(cargo).State = EntityState.Modified;
            context.SaveChanges();
        }
        
        public void DeleteCargo(int id)
        {
            var existe = context.CARGOes.Where(p => p.Id.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                context.CARGOes.Remove(existe); 
                context.SaveChanges();
            }
        }


    }
}
